{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "'0.16'"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz\n",
    "graphviz.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "# 创建数据集\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "def createDataSet():\n",
    "    row_data = {'accompany':[1,1,1,0,0],\n",
    "                'game':[1,1,0,1,1],\n",
    "                'education':[0,2,1,2,2],\n",
    "                'label':['1','1','0','0','0']}\n",
    "    dataSet = pd.DataFrame(row_data)\n",
    "    return dataSet"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "   accompany  game  education label\n0          1     1          0     1\n1          1     1          2     1\n2          1     0          1     0\n3          0     1          2     0\n4          0     1          2     0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>accompany</th>\n      <th>game</th>\n      <th>education</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSet = createDataSet()\n",
    "dataSet"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "label=dataSet.iloc[:,-1]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "0    3\n1    2\nName: label, dtype: int64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label.value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "p=label.value_counts()/label.shape[0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "ent=(-p*np.log2(p)).sum()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "def calEnt(dataSet):\n",
    "    label=dataSet.iloc[:,-1]\n",
    "    p=label.value_counts()/label.shape[0]\n",
    "    ent=(-p*np.log2(p)).sum()\n",
    "    return ent"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "函数功能：根据信息增益选择出最佳数据集切分的列\n",
    "参数说明：\n",
    " dataSet：原始数据集\n",
    "返回：\n",
    " axis：数据集最佳切分列的索引\n",
    "\"\"\"\n",
    "# 选择最优的列进⾏切分\n",
    "def bestSplit_silly(dataSet):\n",
    "    baseEnt = calEnt(dataSet) # 计算原始熵\n",
    "    bestGain = 0 # 初始化信息增益\n",
    "    axis = -1 # 初始化最佳切分列，标签列\n",
    "    for i in range(dataSet.shape[1]-1): # 对特征的每⼀列进⾏循环\n",
    "        levels= dataSet.iloc[:,i].value_counts().index # 提取出当前列的所有取值\n",
    "        ents = 0 # 初始化⼦节点的信息熵\n",
    "        for j in levels: # 对当前列的每⼀个取值进⾏循环\n",
    "            childSet = dataSet[dataSet.iloc[:,i]==j] # 某⼀个⼦节点的dataframe\n",
    "            ent = calEnt(childSet) # 计算某⼀个⼦节点的信息熵\n",
    "            ents += (childSet.shape[0]/dataSet.shape[0])*ent # 计算当前列的信息熵\n",
    "            #print(f'第{i}列的信息熵为{ents}')\n",
    "            infoGain = baseEnt-ents # 计算当前列的信息增益\n",
    "            #print(f'第{i}列的信息增益为{infoGain}')\n",
    "    if (infoGain > bestGain):\n",
    "        bestGain = infoGain # 选择最⼤信息增益\n",
    "        axis = i # 最⼤信息增益所在列的索引\n",
    "    return axis"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "   accompany  game  education label\n0          1     1          0     1\n1          1     1          2     1\n2          1     0          1     0\n3          0     1          2     0\n4          0     1          2     0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>accompany</th>\n      <th>game</th>\n      <th>education</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSet"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "0.9709505944546686"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baseEnt=calEnt(dataSet)\n",
    "baseEnt"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "[(0,\n     accompany  game  education label\n  3          0     1          2     0\n  4          0     1          2     0),\n (1,\n     accompany  game  education label\n  0          1     1          0     1\n  1          1     1          2     1\n  2          1     0          1     0)]"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col='accompany'\n",
    "list(dataSet.groupby(col))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "accompany\n0    0.000000\n1    0.918296\ndtype: float64"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSet.groupby(col).apply(lambda df:calEnt(df))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "[accompany\n 0    0.000000\n 1    0.918296\n dtype: float64,\n game\n 0    0.0\n 1    1.0\n dtype: float64,\n education\n 0    0.000000\n 1    0.000000\n 2    0.918296\n dtype: float64]"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[dataSet.groupby(col).apply(lambda df:calEnt(df)) for col in dataSet.columns[:-1]]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [],
   "source": [
    "child_entropy=pd.concat([dataSet.groupby(col).apply(lambda df:calEnt(df)) for col in dataSet.columns[:-1]],axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "child_wight=dataSet.iloc[:,:-1].apply(lambda i:i.value_counts(),axis=0)/dataSet.shape[0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "child_wight_entropy=pd.DataFrame(child_entropy.values*child_wight.values).fillna(0).sum(axis=0)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(baseEnt-child_wight_entropy).idxmax()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "# 封装函数\n",
    "def bestSplit(dataSet):\n",
    "    #计算数据总体的信息熵\n",
    "    baseEnt=calEnt(dataSet)\n",
    "    #计算每一列的信息熵\n",
    "    child_entropy=pd.concat([dataSet.groupby(col).apply(lambda df:calEnt(df)) for col in dataSet.columns[:-1]],axis=1)\n",
    "    #计算每种数占用的比重\n",
    "    child_wight=dataSet.iloc[:,:-1].apply(lambda i:i.value_counts(),axis=0)/dataSet.shape[0]\n",
    "    #得到分类之后的信息熵\n",
    "    child_wight_entropy=pd.DataFrame(child_entropy.values*child_wight.values).fillna(0).sum(axis=0)\n",
    "    #得到信息熵差值最小的信息熵\n",
    "    return (baseEnt-child_wight_entropy).idxmax()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bestSplit(dataSet)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "def mySplit(dataSet,axis,value):\n",
    "    col=dataSet.columns[axis]\n",
    "    redataSet=dataSet.loc[dataSet[col]==value,:].drop(col,axis=1)\n",
    "    return redataSet"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "函数功能：基于最⼤信息增益切分数据集，递归构建决策树\n",
    "参数说明：\n",
    " dataSet：原始数据集(最有⼀列是标签)\n",
    "返回：\n",
    " myTree：字典形式的树\n",
    "\"\"\"\n",
    "def createTree(dataSet):\n",
    "    featlist = list(dataSet.columns) # 提取出数据集所有的列\n",
    "    classlist = dataSet.iloc[:,-1].value_counts() # 获取最后⼀列类标签\n",
    "    # 判断最多标签数⽬是否等于数据集⾏数，或者数据集是否只有⼀列\n",
    "    if classlist.values[0]==dataSet.shape[0] or dataSet.shape[1] == 1:\n",
    "        return classlist.index[0] # 如果是，返回类标签\n",
    "    axis = bestSplit(dataSet) # 确定出当前最佳切分列的索引\n",
    "    bestfeat = featlist[axis] # 获取该索引对应的特征\n",
    "    myTree = {bestfeat:{}} # 采⽤字典嵌套的⽅式存储树信息\n",
    "    del featlist[axis] # 删除当前特征\n",
    "    valuelist = set(dataSet.iloc[:,axis]) # 提取最佳切分列所有属性值\n",
    "    for value in valuelist: # 对每⼀个属性值递归建树\n",
    "        myTree[bestfeat][value] = createTree(mySplit(dataSet,axis,value))\n",
    "    return myTree"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "{'accompany': {0: '0', 1: {'game': {0: '0', 1: '1'}}}}"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "createTree(dataSet)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "   accompany  game  education label  salary\n0          1     1          0     1      20\n1          1     1          2     1     100\n2          1     0          1     0      30\n3          0     1          2     0      10\n4          0     1          2     0      15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>accompany</th>\n      <th>game</th>\n      <th>education</th>\n      <th>label</th>\n      <th>salary</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n      <td>100</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSet['salary']=[20,100,30,10,15]\n",
    "dataSet"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    " # 导入数据集\n",
    "from sklearn import tree\n",
    "from sklearn.tree import DecisionTreeClassifier as DTC\n",
    "from sklearn.datasets import load_wine\n",
    "from sklearn.model_selection import train_test_split,cross_val_score"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [],
   "source": [
    "wine=load_wine()\n",
    "X=wine['data']\n",
    "Y=wine['target']"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "     alcohol  malic_acid   ash  alcalinity_of_ash  magnesium  total_phenols  \\\n0      14.23        1.71  2.43               15.6      127.0           2.80   \n1      13.20        1.78  2.14               11.2      100.0           2.65   \n2      13.16        2.36  2.67               18.6      101.0           2.80   \n3      14.37        1.95  2.50               16.8      113.0           3.85   \n4      13.24        2.59  2.87               21.0      118.0           2.80   \n..       ...         ...   ...                ...        ...            ...   \n173    13.71        5.65  2.45               20.5       95.0           1.68   \n174    13.40        3.91  2.48               23.0      102.0           1.80   \n175    13.27        4.28  2.26               20.0      120.0           1.59   \n176    13.17        2.59  2.37               20.0      120.0           1.65   \n177    14.13        4.10  2.74               24.5       96.0           2.05   \n\n     flavanoids  nonflavanoid_phenols  proanthocyanins  color_intensity   hue  \\\n0          3.06                  0.28             2.29             5.64  1.04   \n1          2.76                  0.26             1.28             4.38  1.05   \n2          3.24                  0.30             2.81             5.68  1.03   \n3          3.49                  0.24             2.18             7.80  0.86   \n4          2.69                  0.39             1.82             4.32  1.04   \n..          ...                   ...              ...              ...   ...   \n173        0.61                  0.52             1.06             7.70  0.64   \n174        0.75                  0.43             1.41             7.30  0.70   \n175        0.69                  0.43             1.35            10.20  0.59   \n176        0.68                  0.53             1.46             9.30  0.60   \n177        0.76                  0.56             1.35             9.20  0.61   \n\n     od280/od315_of_diluted_wines  proline  label  \n0                            3.92   1065.0      0  \n1                            3.40   1050.0      0  \n2                            3.17   1185.0      0  \n3                            3.45   1480.0      0  \n4                            2.93    735.0      0  \n..                            ...      ...    ...  \n173                          1.74    740.0      2  \n174                          1.56    750.0      2  \n175                          1.56    835.0      2  \n176                          1.62    840.0      2  \n177                          1.60    560.0      2  \n\n[178 rows x 14 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>alcohol</th>\n      <th>malic_acid</th>\n      <th>ash</th>\n      <th>alcalinity_of_ash</th>\n      <th>magnesium</th>\n      <th>total_phenols</th>\n      <th>flavanoids</th>\n      <th>nonflavanoid_phenols</th>\n      <th>proanthocyanins</th>\n      <th>color_intensity</th>\n      <th>hue</th>\n      <th>od280/od315_of_diluted_wines</th>\n      <th>proline</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>14.23</td>\n      <td>1.71</td>\n      <td>2.43</td>\n      <td>15.6</td>\n      <td>127.0</td>\n      <td>2.80</td>\n      <td>3.06</td>\n      <td>0.28</td>\n      <td>2.29</td>\n      <td>5.64</td>\n      <td>1.04</td>\n      <td>3.92</td>\n      <td>1065.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>13.20</td>\n      <td>1.78</td>\n      <td>2.14</td>\n      <td>11.2</td>\n      <td>100.0</td>\n      <td>2.65</td>\n      <td>2.76</td>\n      <td>0.26</td>\n      <td>1.28</td>\n      <td>4.38</td>\n      <td>1.05</td>\n      <td>3.40</td>\n      <td>1050.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>13.16</td>\n      <td>2.36</td>\n      <td>2.67</td>\n      <td>18.6</td>\n      <td>101.0</td>\n      <td>2.80</td>\n      <td>3.24</td>\n      <td>0.30</td>\n      <td>2.81</td>\n      <td>5.68</td>\n      <td>1.03</td>\n      <td>3.17</td>\n      <td>1185.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>14.37</td>\n      <td>1.95</td>\n      <td>2.50</td>\n      <td>16.8</td>\n      <td>113.0</td>\n      <td>3.85</td>\n      <td>3.49</td>\n      <td>0.24</td>\n      <td>2.18</td>\n      <td>7.80</td>\n      <td>0.86</td>\n      <td>3.45</td>\n      <td>1480.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>13.24</td>\n      <td>2.59</td>\n      <td>2.87</td>\n      <td>21.0</td>\n      <td>118.0</td>\n      <td>2.80</td>\n      <td>2.69</td>\n      <td>0.39</td>\n      <td>1.82</td>\n      <td>4.32</td>\n      <td>1.04</td>\n      <td>2.93</td>\n      <td>735.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>173</th>\n      <td>13.71</td>\n      <td>5.65</td>\n      <td>2.45</td>\n      <td>20.5</td>\n      <td>95.0</td>\n      <td>1.68</td>\n      <td>0.61</td>\n      <td>0.52</td>\n      <td>1.06</td>\n      <td>7.70</td>\n      <td>0.64</td>\n      <td>1.74</td>\n      <td>740.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>174</th>\n      <td>13.40</td>\n      <td>3.91</td>\n      <td>2.48</td>\n      <td>23.0</td>\n      <td>102.0</td>\n      <td>1.80</td>\n      <td>0.75</td>\n      <td>0.43</td>\n      <td>1.41</td>\n      <td>7.30</td>\n      <td>0.70</td>\n      <td>1.56</td>\n      <td>750.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>175</th>\n      <td>13.27</td>\n      <td>4.28</td>\n      <td>2.26</td>\n      <td>20.0</td>\n      <td>120.0</td>\n      <td>1.59</td>\n      <td>0.69</td>\n      <td>0.43</td>\n      <td>1.35</td>\n      <td>10.20</td>\n      <td>0.59</td>\n      <td>1.56</td>\n      <td>835.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>176</th>\n      <td>13.17</td>\n      <td>2.59</td>\n      <td>2.37</td>\n      <td>20.0</td>\n      <td>120.0</td>\n      <td>1.65</td>\n      <td>0.68</td>\n      <td>0.53</td>\n      <td>1.46</td>\n      <td>9.30</td>\n      <td>0.60</td>\n      <td>1.62</td>\n      <td>840.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>177</th>\n      <td>14.13</td>\n      <td>4.10</td>\n      <td>2.74</td>\n      <td>24.5</td>\n      <td>96.0</td>\n      <td>2.05</td>\n      <td>0.76</td>\n      <td>0.56</td>\n      <td>1.35</td>\n      <td>9.20</td>\n      <td>0.61</td>\n      <td>1.60</td>\n      <td>560.0</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n<p>178 rows × 14 columns</p>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.DataFrame(X,columns=wine.feature_names)\n",
    "data['label']=Y\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "(1.0, 0.9629629629629629)"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切分数据集\n",
    "Xtrain,Xtest,Ytrain,Ytest=train_test_split(X,Y,test_size=0.3)\n",
    "# 实例化模型\n",
    "clf=DTC()\n",
    "# 训练模型\n",
    "clf.fit(Xtrain,Ytrain)\n",
    "# 查看训练集和测试集的分数\n",
    "clf.score(Xtrain,Ytrain),clf.score(Xtest,Ytest)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [],
   "source": [
    "?DTC"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "<graphviz.files.Source at 0x21651b55940>",
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n -->\r\n<!-- Title: Tree Pages: 1 -->\r\n<svg width=\"1080pt\" height=\"671pt\"\r\n viewBox=\"0.00 0.00 1080.00 671.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 667)\">\r\n<title>Tree</title>\r\n<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-667 1076,-667 1076,4 -4,4\"/>\r\n<!-- 0 -->\r\n<g id=\"node1\" class=\"node\"><title>0</title>\r\n<path fill=\"#ecfcf3\" stroke=\"black\" d=\"M587,-663C587,-663 474,-663 474,-663 468,-663 462,-657 462,-651 462,-651 462,-592 462,-592 462,-586 468,-580 474,-580 474,-580 587,-580 587,-580 593,-580 599,-586 599,-592 599,-592 599,-651 599,-651 599,-657 593,-663 587,-663\"/>\r\n<text text-anchor=\"middle\" x=\"530.5\" y=\"-647.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">proline &lt;= 755.0</text>\r\n<text text-anchor=\"middle\" x=\"530.5\" y=\"-632.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.656</text>\r\n<text text-anchor=\"middle\" x=\"530.5\" y=\"-617.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 124</text>\r\n<text text-anchor=\"middle\" x=\"530.5\" y=\"-602.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [42, 50, 32]</text>\r\n<text text-anchor=\"middle\" x=\"530.5\" y=\"-587.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 1 -->\r\n<g id=\"node2\" class=\"node\"><title>1</title>\r\n<path fill=\"#b6f5d1\" stroke=\"black\" d=\"M502,-544C502,-544 247,-544 247,-544 241,-544 235,-538 235,-532 235,-532 235,-473 235,-473 235,-467 241,-461 247,-461 247,-461 502,-461 502,-461 508,-461 514,-467 514,-473 514,-473 514,-532 514,-532 514,-538 508,-544 502,-544\"/>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-528.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">od280/od315_of_diluted_wines &lt;= 1.925</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-513.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.498</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-498.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 78</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-483.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 47, 29]</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-468.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 0&#45;&gt;1 -->\r\n<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M476.377,-579.907C463.55,-570.288 449.771,-559.953 436.62,-550.09\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"438.628,-547.221 428.528,-544.021 434.428,-552.821 438.628,-547.221\"/>\r\n<text text-anchor=\"middle\" x=\"432.063\" y=\"-565.07\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n</g>\r\n<!-- 12 -->\r\n<g id=\"node13\" class=\"node\"><title>12</title>\r\n<path fill=\"#e99355\" stroke=\"black\" d=\"M814,-544C814,-544 559,-544 559,-544 553,-544 547,-538 547,-532 547,-532 547,-473 547,-473 547,-467 553,-461 559,-461 559,-461 814,-461 814,-461 820,-461 826,-467 826,-473 826,-473 826,-532 826,-532 826,-538 820,-544 814,-544\"/>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-528.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">od280/od315_of_diluted_wines &lt;= 2.165</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-513.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.235</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-498.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 46</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-483.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [40, 3, 3]</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-468.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 琴酒</text>\r\n</g>\r\n<!-- 0&#45;&gt;12 -->\r\n<g id=\"edge12\" class=\"edge\"><title>0&#45;&gt;12</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M584.623,-579.907C597.45,-570.288 611.229,-559.953 624.38,-550.09\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"626.572,-552.821 632.472,-544.021 622.372,-547.221 626.572,-552.821\"/>\r\n<text text-anchor=\"middle\" x=\"628.937\" y=\"-565.07\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n</g>\r\n<!-- 2 -->\r\n<g id=\"node3\" class=\"node\"><title>2</title>\r\n<path fill=\"#8a48e7\" stroke=\"black\" d=\"M244,-425C244,-425 147,-425 147,-425 141,-425 135,-419 135,-413 135,-413 135,-354 135,-354 135,-348 141,-342 147,-342 147,-342 244,-342 244,-342 250,-342 256,-348 256,-354 256,-354 256,-413 256,-413 256,-419 250,-425 244,-425\"/>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">hue &lt;= 0.97</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.128</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 29</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 2, 27]</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = ⻉尔摩德</text>\r\n</g>\r\n<!-- 1&#45;&gt;2 -->\r\n<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M312.397,-460.907C297.061,-450.884 280.538,-440.083 264.881,-429.849\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"266.57,-426.772 256.285,-424.231 262.74,-432.632 266.57,-426.772\"/>\r\n</g>\r\n<!-- 5 -->\r\n<g id=\"node6\" class=\"node\"><title>5</title>\r\n<path fill=\"#4ae78c\" stroke=\"black\" d=\"M433,-425C433,-425 316,-425 316,-425 310,-425 304,-419 304,-413 304,-413 304,-354 304,-354 304,-348 310,-342 316,-342 316,-342 433,-342 433,-342 439,-342 445,-348 445,-354 445,-354 445,-413 445,-413 445,-419 439,-425 433,-425\"/>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">flavanoids &lt;= 0.895</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.153</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 49</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 45, 2]</text>\r\n<text text-anchor=\"middle\" x=\"374.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 1&#45;&gt;5 -->\r\n<g id=\"edge5\" class=\"edge\"><title>1&#45;&gt;5</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M374.5,-460.907C374.5,-452.649 374.5,-443.864 374.5,-435.302\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"378,-435.021 374.5,-425.021 371,-435.021 378,-435.021\"/>\r\n</g>\r\n<!-- 3 -->\r\n<g id=\"node4\" class=\"node\"><title>3</title>\r\n<path fill=\"#8139e5\" stroke=\"black\" d=\"M109,-298.5C109,-298.5 12,-298.5 12,-298.5 6,-298.5 0,-292.5 0,-286.5 0,-286.5 0,-242.5 0,-242.5 0,-236.5 6,-230.5 12,-230.5 12,-230.5 109,-230.5 109,-230.5 115,-230.5 121,-236.5 121,-242.5 121,-242.5 121,-286.5 121,-286.5 121,-292.5 115,-298.5 109,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"60.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"60.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 27</text>\r\n<text text-anchor=\"middle\" x=\"60.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 27]</text>\r\n<text text-anchor=\"middle\" x=\"60.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = ⻉尔摩德</text>\r\n</g>\r\n<!-- 2&#45;&gt;3 -->\r\n<g id=\"edge3\" class=\"edge\"><title>2&#45;&gt;3</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M148.662,-341.907C135.039,-330.101 120.173,-317.217 106.596,-305.45\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"108.619,-302.571 98.7695,-298.667 104.034,-307.861 108.619,-302.571\"/>\r\n</g>\r\n<!-- 4 -->\r\n<g id=\"node5\" class=\"node\"><title>4</title>\r\n<path fill=\"#39e581\" stroke=\"black\" d=\"M240,-298.5C240,-298.5 151,-298.5 151,-298.5 145,-298.5 139,-292.5 139,-286.5 139,-286.5 139,-242.5 139,-242.5 139,-236.5 145,-230.5 151,-230.5 151,-230.5 240,-230.5 240,-230.5 246,-230.5 252,-236.5 252,-242.5 252,-242.5 252,-286.5 252,-286.5 252,-292.5 246,-298.5 240,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 2, 0]</text>\r\n<text text-anchor=\"middle\" x=\"195.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 2&#45;&gt;4 -->\r\n<g id=\"edge4\" class=\"edge\"><title>2&#45;&gt;4</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M195.5,-341.907C195.5,-331.204 195.5,-319.615 195.5,-308.776\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"199,-308.667 195.5,-298.667 192,-308.667 199,-308.667\"/>\r\n</g>\r\n<!-- 6 -->\r\n<g id=\"node7\" class=\"node\"><title>6</title>\r\n<path fill=\"#8139e5\" stroke=\"black\" d=\"M374.5,-298.5C374.5,-298.5 282.5,-298.5 282.5,-298.5 276.5,-298.5 270.5,-292.5 270.5,-286.5 270.5,-286.5 270.5,-242.5 270.5,-242.5 270.5,-236.5 276.5,-230.5 282.5,-230.5 282.5,-230.5 374.5,-230.5 374.5,-230.5 380.5,-230.5 386.5,-236.5 386.5,-242.5 386.5,-242.5 386.5,-286.5 386.5,-286.5 386.5,-292.5 380.5,-298.5 374.5,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 2]</text>\r\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = ⻉尔摩德</text>\r\n</g>\r\n<!-- 5&#45;&gt;6 -->\r\n<g id=\"edge6\" class=\"edge\"><title>5&#45;&gt;6</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M358.541,-341.907C354.246,-330.983 349.588,-319.137 345.251,-308.107\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"348.456,-306.693 341.54,-298.667 341.942,-309.254 348.456,-306.693\"/>\r\n</g>\r\n<!-- 7 -->\r\n<g id=\"node8\" class=\"node\"><title>7</title>\r\n<path fill=\"#42e687\" stroke=\"black\" d=\"M524,-306C524,-306 417,-306 417,-306 411,-306 405,-300 405,-294 405,-294 405,-235 405,-235 405,-229 411,-223 417,-223 417,-223 524,-223 524,-223 530,-223 536,-229 536,-235 536,-235 536,-294 536,-294 536,-300 530,-306 524,-306\"/>\r\n<text text-anchor=\"middle\" x=\"470.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">alcohol &lt;= 13.175</text>\r\n<text text-anchor=\"middle\" x=\"470.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.081</text>\r\n<text text-anchor=\"middle\" x=\"470.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 47</text>\r\n<text text-anchor=\"middle\" x=\"470.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 45, 0]</text>\r\n<text text-anchor=\"middle\" x=\"470.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 5&#45;&gt;7 -->\r\n<g id=\"edge7\" class=\"edge\"><title>5&#45;&gt;7</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M407.807,-341.907C415.253,-332.832 423.221,-323.121 430.894,-313.769\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"433.615,-315.972 437.252,-306.021 428.203,-311.531 433.615,-315.972\"/>\r\n</g>\r\n<!-- 8 -->\r\n<g id=\"node9\" class=\"node\"><title>8</title>\r\n<path fill=\"#39e581\" stroke=\"black\" d=\"M449,-179.5C449,-179.5 352,-179.5 352,-179.5 346,-179.5 340,-173.5 340,-167.5 340,-167.5 340,-123.5 340,-123.5 340,-117.5 346,-111.5 352,-111.5 352,-111.5 449,-111.5 449,-111.5 455,-111.5 461,-117.5 461,-123.5 461,-123.5 461,-167.5 461,-167.5 461,-173.5 455,-179.5 449,-179.5\"/>\r\n<text text-anchor=\"middle\" x=\"400.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"400.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 43</text>\r\n<text text-anchor=\"middle\" x=\"400.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 43, 0]</text>\r\n<text text-anchor=\"middle\" x=\"400.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 7&#45;&gt;8 -->\r\n<g id=\"edge8\" class=\"edge\"><title>7&#45;&gt;8</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M446.214,-222.907C439.546,-211.763 432.304,-199.658 425.592,-188.439\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"428.481,-186.451 420.343,-179.667 422.474,-190.045 428.481,-186.451\"/>\r\n</g>\r\n<!-- 9 -->\r\n<g id=\"node10\" class=\"node\"><title>9</title>\r\n<path fill=\"#ffffff\" stroke=\"black\" d=\"M589.5,-187C589.5,-187 491.5,-187 491.5,-187 485.5,-187 479.5,-181 479.5,-175 479.5,-175 479.5,-116 479.5,-116 479.5,-110 485.5,-104 491.5,-104 491.5,-104 589.5,-104 589.5,-104 595.5,-104 601.5,-110 601.5,-116 601.5,-116 601.5,-175 601.5,-175 601.5,-181 595.5,-187 589.5,-187\"/>\r\n<text text-anchor=\"middle\" x=\"540.5\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">alcohol &lt;= 13.29</text>\r\n<text text-anchor=\"middle\" x=\"540.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.5</text>\r\n<text text-anchor=\"middle\" x=\"540.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n<text text-anchor=\"middle\" x=\"540.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 2, 0]</text>\r\n<text text-anchor=\"middle\" x=\"540.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 琴酒</text>\r\n</g>\r\n<!-- 7&#45;&gt;9 -->\r\n<g id=\"edge9\" class=\"edge\"><title>7&#45;&gt;9</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M494.786,-222.907C500.053,-214.105 505.678,-204.703 511.117,-195.612\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"514.126,-197.399 516.257,-187.021 508.119,-193.805 514.126,-197.399\"/>\r\n</g>\r\n<!-- 10 -->\r\n<g id=\"node11\" class=\"node\"><title>10</title>\r\n<path fill=\"#e58139\" stroke=\"black\" d=\"M519,-68C519,-68 430,-68 430,-68 424,-68 418,-62 418,-56 418,-56 418,-12 418,-12 418,-6 424,-0 430,-0 430,-0 519,-0 519,-0 525,-0 531,-6 531,-12 531,-12 531,-56 531,-56 531,-62 525,-68 519,-68\"/>\r\n<text text-anchor=\"middle\" x=\"474.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"474.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n<text text-anchor=\"middle\" x=\"474.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 0, 0]</text>\r\n<text text-anchor=\"middle\" x=\"474.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 琴酒</text>\r\n</g>\r\n<!-- 9&#45;&gt;10 -->\r\n<g id=\"edge10\" class=\"edge\"><title>9&#45;&gt;10</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M515.924,-103.726C510.646,-94.9703 505.061,-85.7032 499.758,-76.9051\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"502.731,-75.0574 494.571,-68.2996 496.736,-78.671 502.731,-75.0574\"/>\r\n</g>\r\n<!-- 11 -->\r\n<g id=\"node12\" class=\"node\"><title>11</title>\r\n<path fill=\"#39e581\" stroke=\"black\" d=\"M650,-68C650,-68 561,-68 561,-68 555,-68 549,-62 549,-56 549,-56 549,-12 549,-12 549,-6 555,-0 561,-0 561,-0 650,-0 650,-0 656,-0 662,-6 662,-12 662,-12 662,-56 662,-56 662,-62 656,-68 650,-68\"/>\r\n<text text-anchor=\"middle\" x=\"605.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"605.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n<text text-anchor=\"middle\" x=\"605.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 2, 0]</text>\r\n<text text-anchor=\"middle\" x=\"605.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 9&#45;&gt;11 -->\r\n<g id=\"edge11\" class=\"edge\"><title>9&#45;&gt;11</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M564.704,-103.726C569.901,-94.9703 575.402,-85.7032 580.625,-76.9051\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"583.638,-78.6853 585.733,-68.2996 577.619,-75.1121 583.638,-78.6853\"/>\r\n</g>\r\n<!-- 13 -->\r\n<g id=\"node14\" class=\"node\"><title>13</title>\r\n<path fill=\"#ab7bee\" stroke=\"black\" d=\"M747.5,-425C747.5,-425 625.5,-425 625.5,-425 619.5,-425 613.5,-419 613.5,-413 613.5,-413 613.5,-354 613.5,-354 613.5,-348 619.5,-342 625.5,-342 625.5,-342 747.5,-342 747.5,-342 753.5,-342 759.5,-348 759.5,-354 759.5,-354 759.5,-413 759.5,-413 759.5,-419 753.5,-425 747.5,-425\"/>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">total_phenols &lt;= 1.3</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.375</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 3]</text>\r\n<text text-anchor=\"middle\" x=\"686.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = ⻉尔摩德</text>\r\n</g>\r\n<!-- 12&#45;&gt;13 -->\r\n<g id=\"edge13\" class=\"edge\"><title>12&#45;&gt;13</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M686.5,-460.907C686.5,-452.649 686.5,-443.864 686.5,-435.302\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"690,-435.021 686.5,-425.021 683,-435.021 690,-435.021\"/>\r\n</g>\r\n<!-- 16 -->\r\n<g id=\"node17\" class=\"node\"><title>16</title>\r\n<path fill=\"#e68743\" stroke=\"black\" d=\"M948.5,-425C948.5,-425 804.5,-425 804.5,-425 798.5,-425 792.5,-419 792.5,-413 792.5,-413 792.5,-354 792.5,-354 792.5,-348 798.5,-342 804.5,-342 804.5,-342 948.5,-342 948.5,-342 954.5,-342 960.5,-348 960.5,-354 960.5,-354 960.5,-413 960.5,-413 960.5,-419 954.5,-425 948.5,-425\"/>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">color_intensity &lt;= 3.435</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.091</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 42</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [40, 2, 0]</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 琴酒</text>\r\n</g>\r\n<!-- 12&#45;&gt;16 -->\r\n<g id=\"edge16\" class=\"edge\"><title>12&#45;&gt;16</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M752.42,-460.907C768.336,-451.106 785.457,-440.563 801.746,-430.533\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"804.017,-433.245 810.697,-425.021 800.347,-427.284 804.017,-433.245\"/>\r\n</g>\r\n<!-- 14 -->\r\n<g id=\"node15\" class=\"node\"><title>14</title>\r\n<path fill=\"#39e581\" stroke=\"black\" d=\"M655,-298.5C655,-298.5 566,-298.5 566,-298.5 560,-298.5 554,-292.5 554,-286.5 554,-286.5 554,-242.5 554,-242.5 554,-236.5 560,-230.5 566,-230.5 566,-230.5 655,-230.5 655,-230.5 661,-230.5 667,-236.5 667,-242.5 667,-242.5 667,-286.5 667,-286.5 667,-292.5 661,-298.5 655,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"610.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"610.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n<text text-anchor=\"middle\" x=\"610.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 0]</text>\r\n<text text-anchor=\"middle\" x=\"610.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 13&#45;&gt;14 -->\r\n<g id=\"edge14\" class=\"edge\"><title>13&#45;&gt;14</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M660.132,-341.907C652.821,-330.652 644.874,-318.418 637.526,-307.106\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"640.427,-305.146 632.044,-298.667 634.557,-308.96 640.427,-305.146\"/>\r\n</g>\r\n<!-- 15 -->\r\n<g id=\"node16\" class=\"node\"><title>15</title>\r\n<path fill=\"#8139e5\" stroke=\"black\" d=\"M789.5,-298.5C789.5,-298.5 697.5,-298.5 697.5,-298.5 691.5,-298.5 685.5,-292.5 685.5,-286.5 685.5,-286.5 685.5,-242.5 685.5,-242.5 685.5,-236.5 691.5,-230.5 697.5,-230.5 697.5,-230.5 789.5,-230.5 789.5,-230.5 795.5,-230.5 801.5,-236.5 801.5,-242.5 801.5,-242.5 801.5,-286.5 801.5,-286.5 801.5,-292.5 795.5,-298.5 789.5,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"743.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"743.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n<text text-anchor=\"middle\" x=\"743.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 3]</text>\r\n<text text-anchor=\"middle\" x=\"743.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = ⻉尔摩德</text>\r\n</g>\r\n<!-- 13&#45;&gt;15 -->\r\n<g id=\"edge15\" class=\"edge\"><title>13&#45;&gt;15</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M706.276,-341.907C711.652,-330.873 717.486,-318.898 722.906,-307.773\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"726.108,-309.19 727.342,-298.667 719.816,-306.124 726.108,-309.19\"/>\r\n</g>\r\n<!-- 17 -->\r\n<g id=\"node18\" class=\"node\"><title>17</title>\r\n<path fill=\"#39e581\" stroke=\"black\" d=\"M921,-298.5C921,-298.5 832,-298.5 832,-298.5 826,-298.5 820,-292.5 820,-286.5 820,-286.5 820,-242.5 820,-242.5 820,-236.5 826,-230.5 832,-230.5 832,-230.5 921,-230.5 921,-230.5 927,-230.5 933,-236.5 933,-242.5 933,-242.5 933,-286.5 933,-286.5 933,-292.5 927,-298.5 921,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 2, 0]</text>\r\n<text text-anchor=\"middle\" x=\"876.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 雪莉</text>\r\n</g>\r\n<!-- 16&#45;&gt;17 -->\r\n<g id=\"edge17\" class=\"edge\"><title>16&#45;&gt;17</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M876.5,-341.907C876.5,-331.204 876.5,-319.615 876.5,-308.776\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"880,-308.667 876.5,-298.667 873,-308.667 880,-308.667\"/>\r\n</g>\r\n<!-- 18 -->\r\n<g id=\"node19\" class=\"node\"><title>18</title>\r\n<path fill=\"#e58139\" stroke=\"black\" d=\"M1060,-298.5C1060,-298.5 963,-298.5 963,-298.5 957,-298.5 951,-292.5 951,-286.5 951,-286.5 951,-242.5 951,-242.5 951,-236.5 957,-230.5 963,-230.5 963,-230.5 1060,-230.5 1060,-230.5 1066,-230.5 1072,-236.5 1072,-242.5 1072,-242.5 1072,-286.5 1072,-286.5 1072,-292.5 1066,-298.5 1060,-298.5\"/>\r\n<text text-anchor=\"middle\" x=\"1011.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n<text text-anchor=\"middle\" x=\"1011.5\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 40</text>\r\n<text text-anchor=\"middle\" x=\"1011.5\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [40, 0, 0]</text>\r\n<text text-anchor=\"middle\" x=\"1011.5\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 琴酒</text>\r\n</g>\r\n<!-- 16&#45;&gt;18 -->\r\n<g id=\"edge18\" class=\"edge\"><title>16&#45;&gt;18</title>\r\n<path fill=\"none\" stroke=\"black\" d=\"M923.338,-341.907C936.961,-330.101 951.827,-317.217 965.404,-305.45\"/>\r\n<polygon fill=\"black\" stroke=\"black\" points=\"967.966,-307.861 973.23,-298.667 963.381,-302.571 967.966,-307.861\"/>\r\n</g>\r\n</g>\r\n</svg>\r\n"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "feature_name = ['酒精','苹果酸','灰','灰的碱性','镁','总酚','类⻩酮','⾮⻩烷类酚类','花⻘素','颜⾊强度','⾊调','od280/od315稀释葡萄酒','脯氨酸']\n",
    "plt.rcParams['font.sans-serif']=['Simhei']\n",
    "plt.rcParams['axes.unicode_minus']=False\n",
    "import graphviz\n",
    "dot_data = tree.export_graphviz(clf\n",
    "                                ,out_file = None\n",
    "                                ,feature_names= wine.feature_names ,class_names=[\"琴酒\",\"雪莉\",\"⻉尔摩德\"] ,filled=True\n",
    "                                ,rounded=True\n",
    "                                )\n",
    "graph = graphviz.Source(dot_data)\n",
    "graph"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "'wine.pdf'"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph.render(\"wine\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}